﻿using _5E.Infrastructure.Collections;
using Melee.Controller.Comet;
using Melee.Controller.Request;
using Melee.Controller.Routing;

namespace Melee.Controller.Handlers
{
    public class SendMessageHandler : RequestHandler
    {
        public SendMessageHandler()
            : base(Routes.SEND_MESSAGE)
        {
        }

        public override void Handle(WebRequest webRequest)
        {
            if (Route.Matches(webRequest))
            {
                CometObserverFactory.Observer.SendMessage(new Message
                {
                    From = webRequest.Cookie.UserName,
                    Value = webRequest.QueryArguments.ExtractFor<string>("message"),
                    Type = webRequest.QueryArguments.ExtractFor<MessageType>("type")
                });
                webRequest.AsyncResult.SetCompleted();
            }
            else
            {
                base.NextHandler.Handle(webRequest);
            }
        }
    }
}
